package org.wenhuayuan.project.main.banner.indicator;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.wenhuayuan.library.util.HiDisplayUtil;
import org.wenhuayuan.project.main.R;
import org.wenhuayuan.uilibrary.banner.core.HiBannerMo;
import org.wenhuayuan.uilibrary.banner.indicator.HiIndicator;

import java.util.List;

public class CircleIndicator extends FrameLayout implements HiIndicator<FrameLayout> {
    List<? extends HiBannerMo> bannerMos;
    private static final int VWC = LayoutParams.WRAP_CONTENT;
    private static final int VMC = LayoutParams.MATCH_PARENT;
    @DrawableRes
    private int pointNormal = R.drawable.shape_point_normal;
    @DrawableRes
    private int pointSelected = R.drawable.shape_point_select;
    private int pointPaddingHorizotal;
    private int pointPaddingVertical;
    public void setBannerMos (List<? extends HiBannerMo> bannerMos) {
        this.bannerMos =  bannerMos;
    }
    public CircleIndicator(@NonNull Context context) {
        this(context, null);
    }

    public CircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    private void initView() {
        pointPaddingHorizotal = HiDisplayUtil.dp2px(5, getResources());
        pointPaddingVertical = HiDisplayUtil.dp2px(15, getResources());
    }

    @Override
    public FrameLayout get() {
        return this;
    }

    @Override
    public void onInflate(int count) {
        removeAllViews();
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setBackgroundColor(Color.parseColor("#33000000"));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams groupParams = new FrameLayout.LayoutParams(VMC, VWC);
        groupParams.gravity = Gravity.BOTTOM|Gravity.CENTER;

        LinearLayout.LayoutParams imParam = new LinearLayout.LayoutParams(VWC, VWC);
        imParam.gravity = Gravity.CENTER_VERTICAL;
        imParam.setMargins(pointPaddingHorizotal, pointPaddingVertical, pointPaddingHorizotal, pointPaddingVertical);

        for (int i = 0; i < count; i ++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setLayoutParams(imParam);
            if(i == 0) {
                imageView.setImageResource(pointSelected);
            } else {
                imageView.setImageResource(pointNormal);
            }

            linearLayout.addView(imageView);
        }
        addView(linearLayout, groupParams);
    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        for (int i = 0; i < count; i ++) {
            ImageView imageView = (ImageView) viewGroup.getChildAt(i);
            if(i == current) {
                imageView.setImageResource(pointSelected);
            } else {
                imageView.setImageResource(pointNormal);
            }
            imageView.requestLayout();
        }
    }
}
